﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using Kugar.Tool.Web.RequestFieldValidation;
using Kugar.UI.Web.AdminLTE.Filters;
using Kugar.UI.Web.AdminLTE.Interfaces;
using Kugar.UI.Web.AdminLTE.PageControls;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;

namespace Kugar.UI.Web.AdminLTE.RequestValidate
{
    public static class GlobalConfigurationHelper
    {
        /// <summary>
        /// 启用AdminLTEUI的Request校验方式组件,可直接向页面中输出客户端校验脚本配置
        /// </summary>
        /// <param name="services"></param>
        public static void UseAdminLTERequestValidate(this IServiceCollection services)
        {
            services.AddSingleton<IClientValidateRender>(new RequestValidateToAdminLte());

            Kugar.Tool.Web.RequestFieldValidation.GlobalConfigurationHelper.UseRequestFieldValidate(services);
        }

        /// <summary>
        /// 是否使用内嵌的静态资源
        /// </summary>
        /// <param name="app"></param>
        public static void UseAdminLTERequestValidateStaticFile(this IApplicationBuilder app)
        {
            app.UseSession();

            var embeddedProvider = new EmbeddedFileProvider(typeof(RequestValidateToAdminLte).Assembly, "Kugar.UI.Web.AdminLTE.RequestValidate");
            
            app.UseStaticFiles(new StaticFileOptions
                               {
                                   FileProvider = embeddedProvider,
                                   RequestPath = new PathString("/requestvalidate"),
                                   OnPrepareResponse = (context) => {
                                       var headers = context.Context.Response.GetTypedHeaders();
                                       headers.CacheControl = new Microsoft.Net.Http.Headers.CacheControlHeaderValue
                                                              {
                                                                  Public = true,
                                                                  MaxAge = TimeSpan.FromDays(365),
                                                                  
                                                              }; 
                                    }
                               });
            
        }

    }
}
